﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgorithmCenter
{
    public class AlgorithmCenter191217
    {
        /// <summary>
        /// https://leetcode-cn.com/problems/zigzag-conversion/
        /// LEETCODEISHIRING
        /// 
        /// L   C   I   R
        /// E T O E S I I G
        /// E   D   H   N
        /// 
        /// LCIRETOESIIGEDHN
        /// 二维数组
        /// </summary>
        /// <param name="s"></param>
        /// <param name="numRows"></param>
        /// <returns></returns>
        public string Convert(string s, int numRows)
        {
            if (numRows == 1)
                return s;
            StringBuilder res = new StringBuilder();
            List<StringBuilder> stringTable = new List<StringBuilder>();

            int curRow = 0;
            bool isDown = true;
            foreach (char c in s)
            {
                if (stringTable.Count < curRow + 1)
                {
                    stringTable.Add(new StringBuilder());
                }
                stringTable[curRow].Append(c);
                //当前行是第一行时
                if (curRow == 0)
                    isDown = true;
                //当前行是最后一行时
                if (curRow == numRows - 1)
                    isDown = false;
                //行数移动
                curRow += isDown ? 1 : -1;
            }
            foreach (StringBuilder stringBuilder in stringTable)
            {
                res.Append(stringBuilder.ToString());
            }

            return res.ToString();
        }
    }
}
